package org.jeecg.modules.smartcampus;

import com.baomidou.mybatisplus.annotation.IdType;

import java.text.SimpleDateFormat;
import java.util.UUID;

/**
 * @author ZYZ
 * @data 2020/8/11 23:59
 */
public class UUIDTest {
        private static final long serialVersionUID = 1L;

        public static String getUUID() {
                return UUID.randomUUID().toString().replace("-", "");
        }

        public static Integer getUUIDInOrderId() {
                Integer orderId = UUID.randomUUID().toString().hashCode();
                orderId = orderId < 0 ? -orderId : orderId; //String.hashCode() 值会为空
                return orderId;
        }

        public static void main(String args[]) {
                /**
                 * 获取不定长随机数
                 *
                 * @author yuyu
                 */
                /*for (int i = 0; i < 100; i++) {
                        System.out.println(UUIDTest.getUUIDInOrderId());
                }*/

                /**
                 * 获取32位随机数
                 *
                 * @author yuyu
                 */
                /*String userId = UUID.randomUUID().toString().replace("-", "");
                System.out.println("userId:" + userId);*/

                /**
                 * 获取19位随机数
                 * 4位年份+11位时间戳+4位随机数
                 *
                 * @author ZYZ
                 */
                //String out=getGuid();
                //System.out.println(getGuid()+"\n"+out.length());

        }
        /**
         * 19位末尾的数字id
         */
        public static volatile int Guid=1000;

        public static String getGuid() {

                UUIDTest.Guid+=1;

                long now = System.currentTimeMillis();
                //获取4位年份数字
                SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy");

                //获取时间戳
                String time=dateFormat.format(now);
                String info=now+"";
                //System.out.println(time+" "+now+"  "+info+"\n");
                //获取三位随机数
                //int ran=(int) ((Math.random()*9+1)*100);
                //要是一段时间内的数据连过大会有重复的情况，所以做以下修改
                int ran=0;
                if(UUIDTest.Guid>9999){
                        UUIDTest.Guid=1000;
                }
                ran=UUIDTest.Guid;

                return time+info.substring(2, info.length())+ran;
        }



}
